package com.binsoft.ndtta;

import android.app.ActionBar;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity
{
	private static final String TAG = "MainActivity";

	private int mWeek = 0;
	private int mTeam = 0;
	private int mDivision = 0;
	private NdttaDatabaseAdapter mDatabaseAdapter = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	
		Bundle extras = getIntent().getExtras();
		
		mTeam = extras.getInt(Constants.TEAM_INTENT_EXTRA);
		mWeek = extras.getInt( Constants.WEEK_INTENT_EXTRA);
		mDivision = extras.getInt( Constants.DIVISION_INTENT_EXTRA);
				
		ActionBar bar = getActionBar();
		
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

		// Initial Contents
		getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FixtureListFragment()).commit();

		// Create access to DB
		mDatabaseAdapter = new NdttaDatabaseAdapter( this );
		mDatabaseAdapter = mDatabaseAdapter.open();		
	}
	
	public NdttaDatabaseAdapter getDBAdapter()
	{
		return mDatabaseAdapter;
	}
	
	public void updateMatch( int week, int team, String homeScore, String awayScore)
	{
		Match match = Matches.getMatch(week, team);
		
		// Get the score and update the match.
		match.homeScore( Integer.parseInt( homeScore ) );
		match.awayScore( Integer.parseInt( awayScore ) );

		// Retrieve the match from the db
		Cursor prevMatch = mDatabaseAdapter.fetchMatch( match,false,true );

		//
		if ( null != prevMatch && prevMatch.getCount() > 0 )
		{
			boolean updated = mDatabaseAdapter.updateMatch(match);
			Log.i(TAG, "Updated:" + updated );
		}
		else
		{
			long row = mDatabaseAdapter.createMatch( match );
			
			Log.i(TAG, "Row:" + row );
		}
	}

	public int getCurrentWeek()
	{
		return mWeek;
	}

	public int getCurrentTeam()
	{
		return mTeam;
	}

	public int getCurrentDivision()
	{
		return mDivision;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.Teams:
	            getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new TeamListFragment()).commit();
        		return true;
	        case R.id.About:
	        	AboutDialog frag = new AboutDialog();
	        	frag.show(getFragmentManager(), "about-dialog");
	            return true;
	        case R.id.Fixtures:
	        	getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FixtureListFragment()).commit();
	        	return true;
	        case R.id.Website:
	        	getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new WebViewFragment()).commit();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
